#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.IO;
using ZO.SmartCore.Collections.Generic;
using ZO.SmartCore.Xml;
using ZO.SmartCore.My4CSharp;

#endregion

namespace ZO.SmartMessenger.UI
{
    /// <summary>
    /// Represent a collection of accounts 
    /// </summary>
    internal class AccountCollection : OrderedDictionary<string, Account>
    {

     
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="AccountCollection"/> class.
        /// </summary>
        public AccountCollection() : base()
        {
            Load();
        }

        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        #endregion

        #region Methods

        /// <summary>
        /// Adds the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Add(Account value)
        {
            if (value != null)
            {
                if (!String.IsNullOrEmpty(value.AccountName))
                {
                    if (base.Contains(value.AccountName))
                    {
                        base[value.AccountName] = value;
                    }
                    else
                    {

                        base.Add(value.AccountName, value);
                    }

                }

            }
        }

        /// <summary>
        /// Removes the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="permanently">if set to <c>true</c> [permanently] remove the account from storage.</param>
        /// <returns></returns>
        public bool Remove(string key, bool permanently)
        {
            if (That.Settings.FileExists(Path.Combine(MessengerHelper.AccountsFolder, key + ".xml")))
            {
                That.Settings.DeleteFile(Path.Combine(MessengerHelper.AccountsFolder, key + ".xml"));
            }
            return base.Remove(key);

        }

        /// <summary>
        /// Loads accounts from disk.
        /// </summary>
        public void Load()
        {
            if (!That.Settings.DirectoryExists(MessengerHelper.AccountsFolder))
            {
                That.Settings.CreateDirectory(MessengerHelper.AccountsFolder);
            }
            string[] fileList = That.Settings.GetFilesNames(MessengerHelper.AccountsFolder , "*.xml");

            if (fileList != null && fileList.Length > 0)
            {

                foreach (string file in fileList)
                {
                    Document xmlDoc = That.Settings.GetXmlDocument(Path.Combine(MessengerHelper.AccountsFolder, file));

                    if (xmlDoc != null)
                    {
                        Account account = new Account();

                        if (xmlDoc.DocumentElement != null)
                        {
                            account.Load(xmlDoc);

                            this.Add(account);
                        }
                        
                    }

                }
            }
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            Document xmlDoc = new Document();
            foreach (Account var in this.Values)
            {
                xmlDoc.LoadXml(var.ToString());
                That.Settings.SaveXmlDocument(Path.Combine(MessengerHelper.AccountsFolder, var.AccountName + ".xml"), xmlDoc);
                xmlDoc.Clear();
            }
        }

        #endregion
    }
}
